// pages/select-time/select-time.js
Page({
  data: {
    invoiceInfo: {},
    yearList: [],
    selectedYear: 2025,
    selectedMonth: '',
    selectedDay: '',
    monthList: [
      { value: '01', label: '1月' },
      { value: '02', label: '2月' },
      { value: '03', label: '3月' },
      { value: '04', label: '4月' },
      { value: '05', label: '5月' },
      { value: '06', label: '6月' },
      { value: '07', label: '7月' },
      { value: '08', label: '8月' },
      { value: '09', label: '9月' },
      { value: '10', label: '10月' },
      { value: '11', label: '11月' },
      { value: '12', label: '12月' }
    ],
    dayList: []
  },

  onLoad: function(options) {
    // 接收发票信息
    this.setData({
      invoiceInfo: {
        id: options.invoiceId,
        type: options.type,
        amount: options.amount,
        date: options.date,
        department: options.department,
        doctor: options.doctor
      }
    });

    // 生成年份列表（近5年）
    const currentYear = 2025;
    const yearList = [];
    for (let i = 0; i < 5; i++) {
      yearList.push(currentYear - i);
    }
    this.setData({ yearList });

    console.log('页面加载完成，年份列表:', yearList);
    console.log('初始状态 - 年份:', this.data.selectedYear, '月份:', this.data.selectedMonth);
    
    // 注意：这里不调用generateDayList，因为还没有选择月份
  },

  // 选择年份
  selectYear: function(e) {
    const year = e.currentTarget.dataset.year;
    console.log('选择年份:', year);
    this.setData({ 
      selectedYear: year,
      selectedMonth: '',
      selectedDay: ''
    });
    this.generateDayList();
  },

  // 选择月份
  selectMonth: function(e) {
    const month = e.currentTarget.dataset.month;
    console.log('选择月份:', month);
    this.setData({ 
      selectedMonth: month,
      selectedDay: ''
    });
    this.generateDayList();
  },

  // 选择日期
  selectDay: function(e) {
    const day = e.currentTarget.dataset.day;
    this.setData({ selectedDay: day });
  },

  // 生成日期列表
  generateDayList: function() {
    const { selectedYear, selectedMonth } = this.data;
    const dayList = [];
    
    if (selectedMonth) {
      // 将字符串月份转换为数字，注意月份从0开始计数
      const monthNum = parseInt(selectedMonth) - 1;
      const daysInMonth = new Date(selectedYear, monthNum + 1, 0).getDate();
      
      console.log(`生成日期列表: 年份=${selectedYear}, 月份=${selectedMonth}, 天数=${daysInMonth}`);
      
      for (let i = 1; i <= daysInMonth; i++) {
        dayList.push({
          value: i.toString().padStart(2, '0'),
          label: i + '日'
        });
      }
      
      console.log(`生成的日期列表:`, dayList);
    }
    
    this.setData({ dayList });
  },

  // 确认申请开票
  confirmInvoice: function() {
    const { selectedYear, selectedMonth, selectedDay, invoiceInfo } = this.data;
    
    if (!selectedMonth || !selectedDay) {
      wx.showToast({
        title: '请选择完整时间',
        icon: 'none'
      });
      return;
    }

    const selectedDate = `${selectedYear}-${selectedMonth}-${selectedDay}`;
    
    wx.navigateTo({
      url: `/pages/invoice-success/invoice-success?invoiceId=${invoiceInfo.id}&type=${invoiceInfo.type}&amount=${invoiceInfo.amount}&date=${selectedDate}&department=${invoiceInfo.department}&doctor=${invoiceInfo.doctor}`
    });
  }
});

